//
//  YLWHomeViewController.m
//  推库iOS
//
//  Created by Mac on 16/2/17.
//  Copyright © 2016年 YLW. All rights reserved.
//

#import "YLWHomeViewController.h"
#import "YLWDetailTextViewController.h"
//#import "YLWSearchViewController.h"
#import "SeminarSonViewController.h"
#import "SeminarSendViewController.h"
#import "MarkChooseViewController.h"
#import "MarkViewController.h"
#import "NoticeViewController.h"
#import "IndexRow.h"
#import "IndexSection.h"
@interface YLWHomeViewController ()
@property (nonatomic, strong) NSArray *listArray;


@end

@implementation YLWHomeViewController

- (NSArray *)listArray{
    if (!_listArray) {
        NSString * path = [[NSBundle mainBundle] pathForResource:@"notice.plist" ofType:nil];
        NSArray * dicArr = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray * arr = [NSMutableArray array];
        for (NSDictionary *dict in dicArr) {
            IndexSection *section = [IndexSection parsingJsonWithDictionary:dict];
            [arr addObject:section];
        }
        _listArray = arr;
    }
    return _listArray;
}

#pragma mark - 视图的生命周期方法
- (void)viewDidLoad {
    [super viewDidLoad];
     self.navigationItem.leftBarButtonItem = nil;
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(edit)];
    self.navigationItem.rightBarButtonItem = item;
//    [self setNav];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(push:) name:@"push" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifPush:) name:@"notifPush" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(menu:) name:@"menu1" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(menu:) name:@"menu2" object:nil];
    self.titleModelArray = [YLWTitleModel titleModelGetModelArrayWith:@"titleArray.plist"];
    
    [self setUI];
    
    
    
}
//这里啊，第一后卫。。。
- (void)notifPush:(NSNotification *)notif {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *rows = [defaults objectForKey:@"rowID"];
    NSLog(@"ZHESHI ID--------%@",rows);
    NSInteger num = [rows intValue];
    IndexSection *section = self.listArray[0];
    IndexRow *row = section.rows [num];
    NoticeViewController *notice =[[NoticeViewController alloc]init];
    notice.inderow = row;
  
    [self.navigationController pushViewController:notice animated:YES];
}

- (void)menu:(NSNotification *)notif {

    if ([notif.name isEqualToString:@"menu1"]) {
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(edit)];
        self.navigationItem.rightBarButtonItem = item;
    }else {
        
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(comment)];
        
        self.navigationItem.rightBarButtonItem = item;
    }
    
}


- (void)edit {
    dispatch_async(dispatch_get_main_queue(), ^{
        
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Seminar" bundle:nil];
        SeminarSendViewController *vc = [story instantiateViewControllerWithIdentifier:@"SeminarSendViewController"];
        [self.navigationController pushViewController:vc animated:YES];
    });
}

- (void)comment {
    MarkViewController *vc = [[MarkViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)push:(id *)arr {
    
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Seminar" bundle:nil];
    SeminarSonViewController *vc = [story instantiateViewControllerWithIdentifier:@"SeminarSonViewController"];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}



#pragma mark - 设置导航栏
/**
 *  设置导航栏
 */
-(void)setNav{

    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"search"] style:UIBarButtonItemStylePlain target:self action:@selector(searchBtn)];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"more1"] style:UIBarButtonItemStylePlain target:self action:@selector(moreBtn)];
    
    self.navigationItem.leftBarButtonItem = leftItem;
    self.navigationItem.rightBarButtonItem = rightItem;

}
///**
// *  导航栏搜索(左侧)按钮
// */
//-(void)searchBtn{
//    NSLog(@"搜索");
//    YLWSearchViewController *searchVC = [[YLWSearchViewController alloc]init];
//    
//    YLWNavigationController *nav = [[YLWNavigationController alloc]initWithRootViewController:searchVC];
//    
//    [self.navigationController presentViewController:nav animated:YES completion:nil];
//
//}
/**
 *  导航栏更多(右侧)按钮
 */
-(void)moreBtn{

    NSLog(@"更多");
}

//#pragma mark - push到详细页面的通知方法
//-(void)pushToDetailText:(NSNotification *)notification{
//
//    YLWDetailTextViewController *detail = [[YLWDetailTextViewController alloc]init];
//    
//    detail.hidesBottomBarWhenPushed = YES;
//    
//    detail.detailTextId = notification.userInfo[YLWDetailTextIdKeyd];
//    
//    [self.navigationController pushViewController:detail animated:YES];
//    
//
//}


#pragma mark - contentCollectionView的代理方法和数据源方法


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
